//
//  JWTChargeVipListViewController.m
//  JWT
//
//  Created by ndm on 17/2/12.
//  Copyright © 2017年 com.jwtsoft. All rights reserved.
//

#import "JWTChargeVipListViewController.h"
#import "JWTChargeVipListCell.h"
#import "JWTCard.h"


@interface JWTChargeVipListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) NSArray *cardArray;

@end

static NSString *chargeVipCell_Identifier = @"chargeVipCell_Identifier";
@implementation JWTChargeVipListViewController

-(instancetype)init{
    self = [super init];
    if (self) {
        _cardArray = [[NSArray alloc] init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initWithNavTitle:@"我的会员卡"];
    
    [self initWithTableView];
    
    [self reloadData];
}

-(void) initWithTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(self.view.left, self.view.top, UISCREEN_WIDTH, UISCREEN_HEIGHT-64-54)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[JWTChargeVipListCell class] forCellReuseIdentifier:chargeVipCell_Identifier];
}

-(void) reloadData{
    NSString *urlString = [[JWTAPPHttpsUrl sharedInstance] getJWTgetCardRecord];
    [HUD showUIBlockingIndicator];
    [AFNHttpsRequest requestWithUrl:urlString WithParam:nil WithGetDataDidFinishBlock:^(id model, NSURLSessionDataTask *task, NSError *error) {
        [HUD hideUIBlockingIndicator];
        if (model) {
            if ([[model objectForKey:@"code"]integerValue] == 200) {
                
                NSLog(@"%@",model);
                
                _cardArray = [model objectForKey:@"data"];
                [_tableView reloadData];
            }
        }
    }];
}


#pragma mark - 表格视图数据源代理方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _cardArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JWTChargeVipListCell *cell = [tableView dequeueReusableCellWithIdentifier:chargeVipCell_Identifier forIndexPath:indexPath];
    JWTCard *card = [JWTCard mj_objectWithKeyValues:[_cardArray objectAtIndex:indexPath.row]];
    cell.vipTypeLabel.text = card.cardname;
    cell.chargeDateLabel.text = card.paytime;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return  cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    static JWTChargeVipListCell *cell = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        cell = [tableView dequeueReusableCellWithIdentifier:chargeVipCell_Identifier];
    });
    return cell.height;
}


@end
